home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / VertexShader / vertexshader.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  17.5 KB  |  430 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Vertex Blend"
  4.    ClientHeight    =   4485
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   5640
  8.    Icon            =   "vertexshader.frx":0000
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   299
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   376
  13.    StartUpPosition =   3  'Windows Default
  14. Attribute VB_Name = "Form1"
  15. Attribute VB_GlobalNameSpace = False
  16. Attribute VB_Creatable = False
  17. Attribute VB_PredeclaredId = True
  18. Attribute VB_Exposed = False
  19. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  20. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  21. '  File:       VertexShader.frm
  22. '  Content:    Example code showing how to use vertex shaders in D3D.
  23. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  24. Option Explicit
  25. ' Scene
  26. Dim m_VB As Direct3DVertexBuffer8
  27. Dim m_IB As Direct3DIndexBuffer8
  28. Dim m_NumVertices As Long
  29. Dim m_NumIndices As Long
  30. Dim m_Shader As Long
  31. Dim m_Size As Long
  32. ' Transforms
  33. Dim m_matPosition As D3DMATRIX
  34. Dim m_matView As D3DMATRIX
  35. Dim m_matProj As D3DMATRIX
  36. 'Navigation
  37. Dim m_bKey(256) As Boolean
  38. Dim m_fSpeed As Single
  39. Dim m_fAngularSpeed As Single
  40. Dim m_vVelocity As D3DVECTOR
  41. Dim m_vAngularVelocity As D3DVECTOR
  42. 'Shader
  43. Dim m_Decl(3) As Long
  44. Dim m_ShaderArray() As Long
  45. Dim m_bInit As Boolean                  ' Indicates that d3d has been initialized
  46. Dim m_bMinimized As Boolean             ' Indicates that display window is minimized
  47. '-----------------------------------------------------------------------------
  48. ' Name: Form_Load()
  49. ' Desc:
  50. '-----------------------------------------------------------------------------
  51. Private Sub Form_Load()
  52.     Me.Show
  53.     DoEvents
  54.     'setup defaults
  55.     Init
  56.     ' Initialize D3D
  57.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  58.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  59.     ' If all fail it will display a message box indicating so.
  60.     '
  61.     m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Me)
  62.     If Not (m_bInit) Then End
  63.     ' Create new D3D vertexbuffer objects and vertex shader
  64.     InitDeviceObjects
  65.     ' Sets the state for those objects and the current D3D device
  66.     RestoreDeviceObjects
  67.     ' Start our timer
  68.     DXUtil_Timer TIMER_start
  69.     ' Run the simulation forever
  70.     ' See Form_Keydown for exit processing
  71.     Do While True
  72.         ' Increment the simulation
  73.         FrameMove
  74.         
  75.         ' Render one image of the simulation
  76.         If Render Then
  77.         
  78.             ' Present the image to the screen
  79.             D3DUtil_PresentAll g_focushwnd
  80.         End If
  81.         
  82.         ' Allow for events to get processed
  83.         DoEvents
  84.         
  85.     Loop
  86. End Sub
  87. '-----------------------------------------------------------------------------
  88. ' Name: Form_Unload()
  89. ' Desc:
  90. '-----------------------------------------------------------------------------
  91. Private Sub Form_Unload(Cancel As Integer)
  92.     DeleteDeviceObjects
  93.     End
  94. End Sub
  95. '-----------------------------------------------------------------------------
  96. ' Name: Init()
  97. ' Desc: Sets attributes for the app.
  98. '-----------------------------------------------------------------------------
  99. Sub Init()
  100.     Me.Caption = "VertexShader"
  101.     Set m_IB = Nothing
  102.     Set m_VB = Nothing
  103.     m_Size = 32
  104.     m_NumIndices = (m_Size - 1) * (m_Size - 1) * 6
  105.     m_NumVertices = m_Size * m_Size
  106.     m_Shader = 0
  107.     m_fSpeed = 5
  108.     m_fAngularSpeed = 1
  109.     m_vVelocity = vec3(0, 0, 0)
  110.     m_vAngularVelocity = vec3(0, 0, 0)
  111.     ' Setup the view matrix
  112.     Dim veye As D3DVECTOR, vat As D3DVECTOR, vUp As D3DVECTOR
  113.     veye = vec3(2, 3, 3)
  114.     vat = vec3(0, 0, 0)
  115.     vUp = vec3(0, 1, 0)
  116.     D3DXMatrixLookAtRH m_matView, veye, vat, vUp
  117.     ' Set the position matrix
  118.     Dim det As Single
  119.     D3DXMatrixInverse m_matPosition, det, m_matView
  120. End Sub
  121. '-----------------------------------------------------------------------------
  122. ' Name: FrameMove()
  123. ' Desc: Called once per frame, the call is the entry point for animating
  124. '       the scene.
  125. '-----------------------------------------------------------------------------
  126. Sub FrameMove()
  127.     Dim fSecsPerFrame As Single
  128.     Dim fTime As Single
  129.     Dim det As Single
  130.     fSecsPerFrame = DXUtil_Timer(TIMER_GETELLAPSEDTIME)
  131.     fTime = DXUtil_Timer(TIMER_GETAPPTIME)
  132.     ' Process keyboard input
  133.     Dim vT As D3DVECTOR, vR As D3DVECTOR
  134.     vT = vec3(0, 0, 0)
  135.     vR = vec3(0, 0, 0)
  136.     If (m_bKey(vbKeyA) Or m_bKey(vbKeyNumpad1) Or m_bKey(vbKeyLeft)) Then vT.x = vT.x - 1  ' Slide Left
  137.     If (m_bKey(vbKeyD) Or m_bKey(vbKeyNumpad3) Or m_bKey(vbKeyRight)) Then vT.x = vT.x + 1 ' Slide Right
  138.     If (m_bKey(vbKeyDown)) Then vT.y = vT.y - 1                                      ' Slide Down
  139.     If (m_bKey(vbKeyUp)) Then vT.y = vT.y + 1                                        ' Slide Up
  140.     If (m_bKey(vbKeyW)) Then vT.z = vT.z - 2                                         ' Move Forward
  141.     If (m_bKey(vbKeyS)) Then vT.z = vT.z + 2                                         ' Move Backward
  142.     If (m_bKey(vbKeyNumpad8)) Then vR.x = vR.x - 1                                   ' Pitch Down
  143.     If (m_bKey(vbKeyNumpad2)) Then vR.x = vR.x + 1                                   ' Pitch Up
  144.     If (m_bKey(vbKeyE) Or m_bKey(vbKeyNumpad6)) Then vR.y = vR.y - 1                 ' Turn Right
  145.     If (m_bKey(vbKeyQ) Or m_bKey(vbKeyNumpad4)) Then vR.y = vR.y + 1                 ' Turn Left
  146.     If (m_bKey(vbKeyNumpad9)) Then vR.z = vR.z - 2                                   ' Roll CW
  147.     If (m_bKey(vbKeyNumpad7)) Then vR.z = vR.z + 2                                   ' Roll CCW
  148.     m_vVelocity.x = m_vVelocity.x * 0.9 + vT.x * 0.1
  149.     m_vVelocity.y = m_vVelocity.y * 0.9 + vT.y * 0.1
  150.     m_vVelocity.z = m_vVelocity.z * 0.9 + vT.z * 0.1
  151.     m_vAngularVelocity.x = m_vAngularVelocity.x * 0.9 + vR.x * 0.1
  152.     m_vAngularVelocity.y = m_vAngularVelocity.x * 0.9 + vR.y * 0.1
  153.     m_vAngularVelocity.z = m_vAngularVelocity.x * 0.9 + vR.z * 0.1
  154.     ' Update position and view matricies
  155.     Dim matT As D3DMATRIX, matR As D3DMATRIX, qR As D3DQUATERNION
  156.     D3DXVec3Scale vT, m_vVelocity, fSecsPerFrame * m_fSpeed
  157.     D3DXVec3Scale vR, m_vAngularVelocity, fSecsPerFrame * m_fAngularSpeed
  158.     D3DXMatrixTranslation matT, vT.x, vT.y, vT.z
  159.     D3DXMatrixMultiply m_matPosition, matT, m_matPosition
  160.     D3DXQuaternionRotationYawPitchRoll qR, vR.y, vR.x, vR.z
  161.     D3DXMatrixRotationQuaternion matR, qR
  162.     D3DXMatrixMultiply m_matPosition, matR, m_matPosition
  163.     D3DXMatrixInverse m_matView, det, m_matPosition
  164.     g_dev.SetTransform D3DTS_VIEW, m_matView
  165.     ' Set up the vertex shader constants
  166.     Dim mat As D3DMATRIX
  167.     Dim vA As D3DVECTOR4, vD As D3DVECTOR4
  168.     Dim vSin As D3DVECTOR4, vCos As D3DVECTOR4
  169.     D3DXMatrixMultiply mat, m_matView, m_matProj
  170.     D3DXMatrixTranspose mat, mat
  171.     vA = vec4(Sin(fTime) * 15, 0, 0.5, 1)
  172.     vD = vec4(g_pi, 1 / (2 * g_pi), 2 * g_pi, 0.05)
  173.     ' Taylor series coefficients for sin and cos
  174.     vSin = vec4(1, -1 / 6, 1 / 120, -1 / 5040)
  175.     vCos = vec4(1, -1 / 2, 1 / 24, -1 / 720)
  176.     g_dev.SetVertexShaderConstant 0, mat, 4
  177.     g_dev.SetVertexShaderConstant 4, vA, 1
  178.     g_dev.SetVertexShaderConstant 7, vD, 1
  179.     g_dev.SetVertexShaderConstant 10, vSin, 1
  180.     g_dev.SetVertexShaderConstant 11, vCos, 1
  181. End Sub
  182. '-----------------------------------------------------------------------------
  183. ' Name: Render()
  184. ' Desc: Called once per frame, the call is the entry point for 3d
  185. '       rendering. This function sets up render states, clears the
  186. '       viewport, and renders the scene.
  187. '-----------------------------------------------------------------------------
  188. Function Render() As Boolean
  189.     Dim v2 As D3DVECTOR2
  190.     Dim hr As Long
  191.     Render = False
  192.     'See what state the device is in.
  193.     hr = g_dev.TestCooperativeLevel
  194.     If hr = D3DERR_DEVICENOTRESET Then
  195.         g_dev.Reset g_d3dpp
  196.         RestoreDeviceObjects
  197.     End If
  198.     'dont bother rendering if we are not ready yet
  199.     If hr <> 0 Then Exit Function
  200.     Render = True
  201.     'Clear the scene
  202.     D3DUtil_ClearAll &HFF&
  203.     With g_dev
  204.         ' Begin the scene
  205.         .BeginScene
  206.         
  207.         .SetVertexShader m_Shader
  208.         .SetStreamSource 0, m_VB, Len(v2)
  209.         .SetIndices m_IB, 0
  210.         
  211.         .DrawIndexedPrimitive D3DPT_TRIANGLELIST, 0, m_NumVertices, _
  212.                                             0, m_NumIndices / 3
  213.         ' End the scene.
  214.         .EndScene
  215.     End With
  216. End Function
  217. '-----------------------------------------------------------------------------
  218. ' Name: RestoreDeviceObjects()
  219. ' Desc: Initialize scene objects.
  220. '-----------------------------------------------------------------------------
  221. Sub InitDeviceObjects()
  222.     Dim Indices() As Integer    'Integer are 4 bytes wide in VB
  223.     Dim Vertices() As D3DVECTOR2
  224.     Dim v As D3DVECTOR2, x As Integer, y As Integer, i As Integer
  225.             
  226.     ' Fill in our index array with triangles indices to make a grid
  227.     ReDim Indices(m_NumIndices)
  228.     For y = 1 To m_Size - 1
  229.         For x = 1 To m_Size - 1
  230.             Indices(i) = (y - 1) * m_Size + (x - 1): i = i + 1
  231.             Indices(i) = (y - 0) * m_Size + (x - 1): i = i + 1
  232.             Indices(i) = (y - 1) * m_Size + (x - 0): i = i + 1
  233.             Indices(i) = (y - 1) * m_Size + (x - 0): i = i + 1
  234.             Indices(i) = (y - 0) * m_Size + (x - 1): i = i + 1
  235.             Indices(i) = (y - 0) * m_Size + (x - 0): i = i + 1
  236.         Next
  237.     Next
  238.     ' Create index buffer and copy the VB array into it
  239.     Set m_IB = g_dev.CreateIndexBuffer(m_NumIndices * 2, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED)
  240.     D3DIndexBuffer8SetData m_IB, 0, m_NumIndices * 2, 0, Indices(0)
  241.     i = 0
  242.         
  243.     'Fill our vertex array with the coordinates of our grid
  244.     ReDim Vertices(m_NumVertices)
  245.     For y = 0 To m_Size - 1
  246.         For x = 0 To m_Size - 1
  247.             Vertices(i) = vec2(((CSng(x) / CSng(m_Size - 1)) - 0.5) * g_pi, _
  248.                             ((CSng(y) / CSng(m_Size - 1)) - 0.5) * g_pi)
  249.                            
  250.             i = i + 1
  251.         Next
  252.     Next
  253.     ' Create a vertex buffer and copy our vertex array into it
  254.     Set m_VB = g_dev.CreateVertexBuffer(m_NumVertices * Len(v), 0, 0, D3DPOOL_MANAGED)
  255.     D3DVertexBuffer8SetData m_VB, 0, m_NumVertices * Len(v), 0, Vertices(0)
  256.     ' Create vertex shader
  257.     Dim strVertexShaderPath As String
  258.     Dim VShaderCode As D3DXBuffer
  259.     m_Decl(0) = D3DVSD_STREAM(0)
  260.     m_Decl(1) = D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT2)
  261.     m_Decl(2) = D3DVSD_END()
  262.         
  263.     ' Find the vertex shader file
  264.     strVertexShaderPath = FindMediaDir("ripple.vsh") + "ripple.vsh"
  265.     'Assemble the vertex shader from the file
  266.     Set VShaderCode = g_d3dx.AssembleShaderFromFile(strVertexShaderPath, 0, "", Nothing)
  267.             
  268.     'Move VShader code into an array
  269.     ReDim m_ShaderArray(VShaderCode.GetBufferSize() / 4)
  270.     g_d3dx.BufferGetData VShaderCode, 0, 1, VShaderCode.GetBufferSize(), m_ShaderArray(0)
  271.     Set VShaderCode = Nothing
  272. End Sub
  273. '-----------------------------------------------------------------------------
  274. ' Name: RestoreDeviceObjects()
  275. ' Desc: Initialize scene objects.
  276. '-----------------------------------------------------------------------------
  277. Sub RestoreDeviceObjects()
  278.     Dim bufferdesc As D3DSURFACE_DESC
  279.     g_dev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO).GetDesc bufferdesc
  280.     ' Set up right handed projection matrix
  281.     Dim fAspectRatio As Single
  282.     fAspectRatio = bufferdesc.width / bufferdesc.height
  283.     D3DXMatrixPerspectiveFovRH m_matProj, 60 * g_pi / 180, fAspectRatio, 0.1, 100
  284.     g_dev.SetTransform D3DTS_PROJECTION, m_matProj
  285.     ' Setup render states
  286.     g_dev.SetRenderState D3DRS_LIGHTING, 0 'FALSE
  287.     g_dev.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
  288.      
  289.     ' Create the vertex shader
  290.     ' NOTE returns value in m_Shader
  291.     g_dev.CreateVertexShader m_Decl(0), m_ShaderArray(0), m_Shader, 0
  292. End Sub
  293. '-----------------------------------------------------------------------------
  294. ' Name: InvalidateDeviceObjects()
  295. ' Desc:
  296. '-----------------------------------------------------------------------------
  297. Sub InvalidateDeviceObjects()
  298.     On Local Error Resume Next
  299.     g_dev.DeleteVertexShader m_Shader
  300. End Sub
  301. '-----------------------------------------------------------------------------
  302. ' Name: DeleteDeviceObjects()
  303. ' Desc: Called when the app is exitting, or the device is being changed,
  304. '       this function deletes any device dependant objects.
  305. '-----------------------------------------------------------------------------
  306. Sub DeleteDeviceObjects()
  307.     Set m_IB = Nothing
  308.     Set m_VB = Nothing
  309.     InvalidateDeviceObjects
  310.     m_bInit = False
  311. End Sub
  312. '-----------------------------------------------------------------------------
  313. ' Name: FinalCleanup()
  314. ' Desc: Called before the app exits, this function gives the app the chance
  315. '       to cleanup after itself.
  316. '-----------------------------------------------------------------------------
  317. Sub FinalCleanup()
  318. End Sub
  319. '-----------------------------------------------------------------------------
  320. ' Name: ConfirmDevice()
  321. ' Desc: Called during device intialization, this code checks the device
  322. '       for some minimum set of capabilities
  323. '-----------------------------------------------------------------------------
  324. Function VerifyDevice(Behavior As Long, format As CONST_D3DFORMAT) As Boolean
  325.     If (Behavior <> D3DCREATE_SOFTWARE_VERTEXPROCESSING) Then
  326.         If (g_d3dCaps.VertexShaderVersion < D3DVS_VERSION(1, 0)) Then Exit Function
  327.     End If
  328.     VerifyDevice = True
  329. End Function
  330. '-----------------------------------------------------------------------------
  331. ' Name: Form_KeyDown()
  332. ' Desc: Process key messages for exit and change device
  333. '-----------------------------------------------------------------------------
  334. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  335.      Dim hr As Long
  336.      
  337.      m_bKey(KeyCode) = True
  338.      Select Case KeyCode
  339.         
  340.         Case vbKeyEscape
  341.             Unload Me
  342.             
  343.         Case vbKeyF2
  344.                 
  345.             ' Pause the timer
  346.             DXUtil_Timer TIMER_STOP
  347.             
  348.             ' Bring up the device selection dialog
  349.             ' we pass in the form so the selection process
  350.             ' can make calls into InitDeviceObjects
  351.             ' and RestoreDeviceObjects
  352.             frmSelectDevice.SelectDevice Me
  353.             
  354.             ' Restart the timer
  355.             DXUtil_Timer TIMER_start
  356.             
  357.         Case vbKeyReturn
  358.         
  359.             ' Check for Alt-Enter if not pressed exit
  360.             If Shift <> 4 Then Exit Sub
  361.             
  362.             ' If we are windowed go fullscreen
  363.             ' If we are fullscreen returned to windowed
  364.             If g_d3dpp.Windowed Then
  365.                  hr = D3DUtil_ResetFullscreen
  366.             Else
  367.                  hr = D3DUtil_ResetWindowed
  368.             End If
  369.                                                           
  370.             If hr = D3DERR_DEVICELOST Then
  371.                 
  372.                 DeleteDeviceObjects
  373.                 
  374.                 m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Me)
  375.                 If Not (m_bInit) Then End
  376.                 
  377.                 InitDeviceObjects
  378.             End If
  379.             
  380.             ' Call Restore after ever mode change
  381.             ' because calling reset looses state that needs to
  382.             ' be reinitialized
  383.             RestoreDeviceObjects
  384.            
  385.     End Select
  386. End Sub
  387. '-----------------------------------------------------------------------------
  388. ' Name: Form_Resize()
  389. ' Desc: hadle resizing of the D3D backbuffer
  390. '-----------------------------------------------------------------------------
  391. Private Sub Form_Resize()
  392.     ' If D3D is not initialized then exit
  393.     If Not m_bInit Then Exit Sub
  394.     ' If we are in a minimized state stop the timer and exit
  395.     If Me.WindowState = vbMinimized Then
  396.         DXUtil_Timer TIMER_STOP
  397.         m_bMinimized = True
  398.         Exit Sub
  399.         
  400.     ' If we just went from a minimized state to maximized
  401.     ' restart the timer
  402.     Else
  403.         If m_bMinimized = True Then
  404.             DXUtil_Timer TIMER_start
  405.             m_bMinimized = False
  406.         End If
  407.     End If
  408.     ' Dont let the window get too small
  409.     If Me.ScaleWidth < 10 Then
  410.         Me.width = Screen.TwipsPerPixelX * 10
  411.         Exit Sub
  412.     End If
  413.     If Me.ScaleHeight < 10 Then
  414.         Me.height = Screen.TwipsPerPixelY * 10
  415.         Exit Sub
  416.     End If
  417.         
  418.     'reset and resize our D3D backbuffer to the size of the window
  419.     D3DUtil_ResizeWindowed Me.hwnd
  420.     'All state get losts after a reset so we need to reinitialze it here
  421.     RestoreDeviceObjects
  422. End Sub
  423. '-----------------------------------------------------------------------------
  424. ' Name: Picture1_KeyUp
  425. ' Desc:
  426. '-----------------------------------------------------------------------------
  427. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  428.     m_bKey(KeyCode) = False
  429. End Sub
  430.